import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:example/app/utils/color_utils.dart';
import 'package:example/index.dart';
import 'package:flutter/material.dart';

import '../search_controller.dart';

class SearchKeywords extends StatelessWidget {
  const SearchKeywords({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchController>(
      builder: (controller) {
        return Container(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Visibility(visible: controller.searchHistory.isNotEmpty,child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    width: Get.infinity,
                    padding: EdgeInsets.all(10.dp),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '搜索历史',
                          style: TextStyle(
                              color: '#333333'.c,
                              fontSize: 14.sp,
                              fontWeight: FontWeight.w400),
                        ),
                        IconButton(
                          icon: Icon(
                            Icons.delete_outline,
                            size: 18.dp,
                            color: Colors.red,
                          ),
                          onPressed: () {
                            AwesomeDialog(
                              context: context,
                              headerAnimationLoop: false,
                              dialogType: DialogType.NO_HEADER,
                              title: '温馨提示',
                              desc:
                              '是否清空历史搜索记录? ',
                              btnOkText: '确定',
                              btnOkOnPress: () {
                                controller.cleanHistory();
                              },
                              btnCancelText: '取消',
                              btnCancelOnPress: (){},
                            )..show();
                          },
                        ),
                      ],
                    ),
                  ),

                  /// 搜索历史
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10.dp),
                    child: Wrap(
                      spacing: 10.dp,
                      runSpacing: 10.dp,
                      children: controller.searchHistory
                          .map(
                            (e) => GestureDetector(
                          onLongPress: () {
                            controller.delHistoryItem(e);
                          },
                          onTap: () {
                            controller.searchController.text = e.toString();
                            controller.keywords.value = e;
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 8.dp, vertical: 3.dp),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(6.dp),
                              color: getRandomColor().withOpacity(0.6),
                            ),
                            child: Text(
                              e.toString(),
                              style: TextStyle(
                                  color: Colors.white, fontSize: 12.sp),
                            ),
                          ),
                        ),
                      )
                          .toList(),
                    ),
                  ),
                  Container(
                    width: Get.infinity,
                    height: 0.5.dp,
                    margin:
                    EdgeInsets.symmetric(horizontal: 15.dp, vertical: 10.dp),
                    color: '#eeeeee'.c,
                  ),
                ],
              ),),
              Container(
                width: Get.infinity,
                padding: EdgeInsets.all(10.dp),
                child: Text(
                  '搜索热词',
                  style: TextStyle(
                      color: '#333333'.c,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w400),
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 10.dp),
                child: Wrap(
                  spacing: 10.dp,
                  runSpacing: 10.dp,
                  children: controller.hotKeywords
                      .map(
                        (e) => GestureDetector(
                          onTap: () {
                            controller.searchController.text = e.name!;
                            controller.keywords.value = e.name!;
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 8.dp, vertical: 3.dp),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(6.dp),
                              color: getRandomColor().withOpacity(0.6),
                            ),
                            child: Text(
                              e.name.toString(),
                              style: TextStyle(
                                  color: Colors.white, fontSize: 12.sp),
                            ),
                          ),
                        ),
                      )
                      .toList(),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
